package com.young.canal.serializer;

import java.io.Serializable;
import java.nio.charset.Charset;

/**
 * @description：
 * @author: yangyahui01
 * @date: 12/11/24 7:03 PM
 */
public class EventHeader implements Serializable {

    private String schemaName;//库名
    private String tableName;//表名
    private int eventType;//时间类型,@see EventType
    private String logFileName;//binlog文件名
    private long logFileOffset;//binlog偏移量
    private long executeTime;//在Slave上执行的binlog时间
    private long storeTime;//存储到kafka或者MQ时的发送时间
    private int sourceType;
    private long slaveId;
    private String sourceAddress;//数据库地址
    private String eventTypeName;
    private String destination;
    private String dbAlias;//通常在分库时使用，默认与schemaName一致，除非特殊设置

    private long parseTime;//binlog被解析的时间

    private static final Charset UTF8 = Charset.forName("UTF-8");

    public String getSchemaName() {
        return schemaName;
    }

    public void setSchemaName(String schemaName) {
        this.schemaName = schemaName;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public int getEventType() {
        return eventType;
    }

    public void setEventType(int eventType) {
        this.eventType = eventType;
    }

    public String getLogFileName() {
        return logFileName;
    }

    public void setLogFileName(String logFileName) {
        this.logFileName = logFileName;
    }

    public long getLogFileOffset() {
        return logFileOffset;
    }

    public void setLogFileOffset(long logFileOffset) {
        this.logFileOffset = logFileOffset;
    }

    public long getExecuteTime() {
        return executeTime;
    }

    public void setExecuteTime(long executeTime) {
        this.executeTime = executeTime;
    }

    public long getStoreTime() {
        return storeTime;
    }

    public void setStoreTime(long storeTime) {
        this.storeTime = storeTime;
    }

    public int getSourceType() {
        return sourceType;
    }

    public void setSourceType(int sourceType) {
        this.sourceType = sourceType;
    }

    public long getSlaveId() {
        return slaveId;
    }

    public void setSlaveId(long slaveId) {
        this.slaveId = slaveId;
    }

    public String getSourceAddress() {
        return sourceAddress;
    }

    public void setSourceAddress(String sourceAddress) {
        this.sourceAddress = sourceAddress;
    }

    public String getEventTypeName() {
        return eventTypeName;
    }

    public void setEventTypeName(String eventTypeName) {
        this.eventTypeName = eventTypeName;
    }

    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }

    public String getDbAlias() {
        return dbAlias;
    }

    public void setDbAlias(String dbAlias) {
        this.dbAlias = dbAlias;
    }

    public long getParseTime() {
        return parseTime;
    }

    public void setParseTime(long parseTime) {
        this.parseTime = parseTime;
    }


    public String toRecordKey() {
        StringBuilder sb = new StringBuilder();
        sb.append(this.getSchemaName())
                .append("#")
                .append(this.getTableName())
                .append("#")
                .append(this.getEventTypeName())
                .append("#")
                .append(this.getLogFileName())
                .append("#")
                .append(this.getLogFileOffset())
                .append("#")
                .append(this.getExecuteTime())
                .append("#")
                .append(this.getDbAlias());
        return sb.toString();
    }

    public static EventHeader fromRecordKey(String recordKey) {
        String[] fields = recordKey.split("#");
        EventHeader header = new EventHeader();
        header.setSchemaName(fields[0]);
        header.setTableName(fields[1]);
        header.setEventTypeName(fields[2]);
        header.setLogFileName(fields[3]);
        header.setLogFileOffset(Long.valueOf(fields[4]));
        header.setExecuteTime(Long.valueOf(fields[5]));
        header.setDbAlias(fields[6]);
        return header;
    }

}
